<!--  -->
<script setup>
import { sentence } from '@/config'
import { useBreadsStore } from '@/stores'
import { onMounted, ref } from 'vue'
import { getLatestNewsAPI } from '@/apis/news'
import moment from 'moment'

const { setBreadCrumbs } = useBreadsStore()
setBreadCrumbs([])

const getRandomSentence = () => sentence[Math.floor(Math.random() * sentence.length)]
const randomSentence = getRandomSentence()
const latestNews = ref({
  title: '',
  content: '',
  createTime: ''
})

onMounted(() => { getLatestNewsAPI().then(res => latestNews.value = res.data).catch() })
</script>

<template>
  <div class="main-container">
    <div class="main-header">
      <div class="main-title">Lifecycle</div>
      <div class="main-subtitle">life is a cycle</div>
    </div>
    <div class="byline">
      <div class="sentence">
        <span>{{ randomSentence }}</span>
      </div>
      <div class="describe">
        <span>{{ $t('main.describe') }}</span>
      </div>
    </div>
    <div class="main-content">
      <p class="centre-text ">{{ $t('main.text1') }}</p>
      <div class="subjects">
        <div class="subj-wrapper">
          <RouterLink to="/article/category" class="subj-link">
            <h5>{{ $t('main.category.article.title') }}</h5>
            <p class="subj-desc">{{ $t('main.category.article.describe') }}</p>
          </RouterLink>
        </div>
        <div class="subj-wrapper">
          <RouterLink to="/essay/newest" class="subj-link">
            <h5>{{ $t('main.category.essay.title') }}</h5>
            <p class="subj-desc">{{ $t('main.category.essay.describe') }}</p>
          </RouterLink>
        </div>
        <div class="subj-wrapper">
          <RouterLink to="/paint" class="subj-link">
            <h5>{{ $t('main.category.picture.title') }}</h5>
            <p class="subj-desc">{{ $t('main.category.picture.describe') }}</p>
          </RouterLink>
        </div>
      </div>
      <div class="divider"></div>
      <p class="centre-text">{{ $t('main.text2') }}</p>
      <div class="subj-wrapper">
        <RouterLink to="/article/MyWeb/3-3" class="subj-link">
          <h5>Update Log</h5>
          <p class="subj-desc">2025/2</p>
        </RouterLink>
      </div>
    </div>
    <div class="main-footer">
      <RouterLink to="news" class="latest-news">
        <span class="news-date">
          {{ latestNews.createAt && moment(latestNews.createAt).format('YYYY-MM-DD') }}
        </span>
        <span class="news-title">{{ latestNews.title }}</span>
      </RouterLink>
      <RouterLink to="about" class="about-link">
        {{ $t('main.about') }}
      </RouterLink>
    </div>
  </div>
</template>

<style scoped lang="scss">
.main-container {
  margin: 50px 0 200px 20px;
  flex: 1;
  width: 60%;
  height: 100%;
  background: var(--site-home-bgc);
  box-shadow: 0px 3px 4px var(--site-home-shadow);
  border: 1px solid var(--border-primary);
  border-radius: 2px;

  @media screen and (max-width: 768px) {
    width: 100%;
    margin-left: 0;
  }

  @media screen and (min-width: 769px) and (max-width: 882px) {
    width: 80%;
    margin-left: 0;
  }

  @media screen and (min-width: 883px) and (max-width: 992px) {
    width: 70%;
    margin-left: 0px;
  }
}

.main-header {
  margin: 10px 0;
  padding-bottom: 10px;
  box-shadow: 0px 4px 7px var(--site-home-header-shadow);
  background-image: var(--site-home-banner);
}

.main-title {
  padding-top: 10px;
  padding-bottom: 0px;
  font-family: "Oswald", "Bookman Old Style", serif;
  font-size: 2.50rem;
  border-bottom: 1px solid var(--site-home-title);
  letter-spacing: 0.12rem;
  width: max-content;
  margin: auto;
  color: var(--site-home-title);
}

.main-subtitle {
  font-family: Roboto, inherit "Merienda", sans-serif;
  font-weight: 400;
  font-size: .75rem;
  color: var(--site-home-subtitle);
  letter-spacing: 0.100rem;
  margin: auto;
  width: max-content;
}

.byline {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  align-items: center;
}

.sentence {
  padding: 10px 0 0;
  opacity: 5%;
  transition: opacity 0.5s linear;

  &:hover {
    opacity: 100%;
  }

  & span {
    padding-left: 10px;
    color: #198a06;
    font-style: italic;
    text-decoration: underline;
    cursor: default;
  }
}

.describe {
  color: var(--text-secondary);
  text-align: right;
  padding: 10px 0 0;

  & span {
    padding: 2px;
    background-color: var(--site-home-describe-bgc);
    border: 1px solid var(--site-home-describe-border);
  }
}

.main-content {
  margin: 10px 10px;
  padding: 10px;
}

.divider {
  width: 40%;
  margin: 8px auto;
  border: 0;
  color: #73827a;
  background: linear-gradient(currentColor, currentColor) no-repeat center;
  background-size: calc(100% - 1.5em - 6px) 1px;
  display: flex;
  justify-content: space-between;

  &::before,
  &::after {
    content: '';
    background-color: #5f866f86;
    display: block;
    width: .75em;
    height: .75em;
    transform: rotate(45deg);
    box-sizing: border-box;
    border: 1px solid;
    margin: 3px;
  }
}

.centre-text {
  text-align: center;
  color: var(--site-home-text-color02);
}

.subjects {
  display: flex;
  justify-content: space-around;
  // flex-shrink: 2;
  flex-wrap: wrap;
  gap: 20px;
}

.subj-wrapper {
  margin: 10px 0;
  flex: 1;
  text-align: center;
  min-width: 200px;
  border: 1px solid var(--border-secondary);
  background-color: var(--site-home-subject-bgc);
  border-radius: 3px;
}

.subj-link {
  display: block;
  padding: 20px 10px;
  text-decoration: none;
  color: var(--site-home-text-color01);
}

.subj-desc {
  font-size: 1.00rem;
  color: rgb(151, 142, 95)
}

.main-footer {
  margin-top: 50px;
  padding: 0 5px;
  display: flex;
  justify-content: space-between;

  @media screen and (max-width: 426px) {
    flex-direction: column;
  }
}

.latest-news {
  text-decoration: none;
  color: rgb(119, 133, 77);
  padding: 0 5px;

  & span:not(:last-child)::after {
    content: "*";
    padding: 0 5px;
  }

  &:hover {
    color: rgb(12, 70, 32);
  }
}

.about-link {
  padding: 0 5px;
  color: rgb(119, 133, 77);

  &:hover {
    color: rgb(12, 70, 32);
  }

  @media screen and (max-width: 426px) {
    border-top: 1px solid var(--border-primary);
  }
}
</style>
